from fastapi import APIRouter, WebSocket, HTTPException, Depends
from typing import List, Optional
from schemas.response import ResponseModel
from core.queue_manager import MessageQueueManager
from core.state_manager import get_state_manager, StateManager

router = APIRouter(prefix="/messages", tags=["messages"])
queue_manager = MessageQueueManager()

@router.post("/", response_model=ResponseModel)
async def send_message(message: str):
    """发送消息到队列"""
    await queue_manager.publish(message, source="http")
    return ResponseModel(
        code=200,
        message="消息发送成功",
        data=message
    )

@router.get("/history", response_model=ResponseModel[List])
async def get_message_history(limit: Optional[int] = None):
    """获取历史消息"""
    history = queue_manager.get_history(limit)
    return ResponseModel(
        code=200,
        message="获取成功",
        data=history
    )

@router.delete("/history", response_model=ResponseModel)
async def clear_message_history():
    """清空历史消息"""
    await queue_manager.clear_history()
    return ResponseModel(
        code=200,
        message="历史消息已清空"
    )

@router.post("/state", response_model=ResponseModel)
async def set_state(
    key: str,
    value: str,
    state_manager: StateManager = Depends(get_state_manager)
):
    """设置状态"""
    state_manager.set(key, value)
    return ResponseModel(
        code=200,
        message="状态设置成功",
        data={key: value}
    )

@router.get("/state/{key}", response_model=ResponseModel)
async def get_state(
    key: str,
    state_manager: StateManager = Depends(get_state_manager)
):
    """获取状态"""
    value = state_manager.get(key)
    return ResponseModel(
        code=200,
        message="获取成功",
        data={key: value}
    ) 